package com.mall.rabbitmq.demo1;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author hyt
 * @date 2021/10/26
 */
@RestController
@RequestMapping("/demo")
public class QueueController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/queue")
    @ResponseBody
    public String sendMsg(String msg){
        rabbitTemplate.convertAndSend("","boot_queue",msg);
        return "发送成功";
    }


    @RequestMapping("/worker")
    @ResponseBody
    public String sendMsg(){
        for(int i= 0;i<20;i++){
            rabbitTemplate.convertAndSend("","boot_worker","msg:"+i);
        }
        return "发送成功";
    }

    //对应所有消息都能接受到 广播消息
    @RequestMapping("/pubsub")
    @ResponseBody
    public String pubsubMsg(String msg){
        rabbitTemplate.convertAndSend("boot_pubsub","","广播消息");
        return "发送成功";
    }

    @RequestMapping("/rounting")
    @ResponseBody
    public String rountingMsg(){
        rabbitTemplate.convertAndSend("boot_rounting_exchange","info","rountinginfo消息");
        rabbitTemplate.convertAndSend("boot_rounting_exchange","error","rountingerror消息");
        return "发送成功";
    }

    @RequestMapping("/topic")
    @ResponseBody
    public String topicMsg(String key){
        rabbitTemplate.convertAndSend("boot_topic_exchange",key,"topic消息");
        return "发送成功";
    }
}
